package com.huawei.hmsdemo10086.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.io.Serializable;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Add_AddressActivity extends AppCompatActivity {

    Button add_button;
    TextView getperson;
    TextView address;
    TextView phone;
    TextView region;
    private TextView mTitleTextView;
    private Button back;
    private SharedPreferences sharedPreferences;
    private String userId;
    private int tag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);

        sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        userId = sharedPreferences.getString("userId",null);
        tag = getIntent().getIntExtra("tag",0);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mTitleTextView.setText("新增收货地址");
        region = (TextView)findViewById(R.id.address_region);
        add_button = (Button)findViewById(R.id.address_save);
        getperson = (TextView) findViewById(R.id.address_username);
        address = (TextView)findViewById(R.id.address_name);
        phone = (TextView)findViewById(R.id.address_phone);
        back = (Button)findViewById(R.id.button_backward);
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save_address();
                if(tag==1)
                {
                    Intent intent = new Intent(Add_AddressActivity.this,AddressActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(Add_AddressActivity.this,AddressActivity.class);
                    startActivity(intent);
                }

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tag==1)
                {
                    Intent intent = new Intent(Add_AddressActivity.this,AddressActivity.class);
                    intent.putExtra("cars",(Serializable)getIntent().getSerializableExtra("cars"));
                    intent.putExtra("tag",1);
                    startActivity(intent);
                }
                else
                {
                    Intent intent = new Intent(Add_AddressActivity.this,AddressActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    public void save_address()
    {
        System.out.println(111);
        String url = "http://10.0.2.2:8443/api/addAddress";
        OkHttpClient mOkHttpClient = new OkHttpClient();

        String json = "{\n" +
                "  \"userid\": \"" + userId + "\",\n" +
                "  \"name\": \"" + getperson.getText().toString() + "\",\n" +
                "  \"address\": \"" + address.getText().toString() + "\",\n" +
                "  \"region\": \"" + region.getText().toString() + "\",\n" +
                "  \"phone\": \"" + phone.getText().toString() + "\"\n" +
                "}";


        Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("application/json"),json)).build();

        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("测试", e+"");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }
}